package wortubung.quiz;

import wortubung.core.LANG;
import wortubung.core.POS;
import wortubung.util.CommonUtils;

public class MultiChoiceQuestion {
	public static final int NONE_OF_ABOVE = -1;

	String word;
	POS pos;
	LANG lang;
	String[] choices;
	int answer;
	transient int index;

	public MultiChoiceQuestion(String word, POS pos, LANG lang, int index) {
		this(word, 4, pos, lang, index);
	}

	public MultiChoiceQuestion(String word, int numChoices, POS pos, LANG lang, int index) {
		this.word = word;
		this.pos = pos;
		this.lang = lang;
		this.choices = new String[numChoices];
		this.answer = NONE_OF_ABOVE;
		this.index = index;
	}

	public String getWord() { return word; }
	public POS getPOS() { return pos; }
	public String[] getChoices() { return choices; }
	public int getAnswer() { return answer; }
	public String getSuffix() {
		if (pos == POS.NOUN_M)
			return "  (m.)";
		if (pos == POS.NOUN_F)
			return "  (f.)";
		if (lang.hasGenderNeuter())
			return "  (nt.)";
		return "";
	}

	/**
	 * This should be called first, i.e., before other addQuestion()'s.
	 */
	public void addAnswer(String answer) {
		this.answer = CommonUtils.random.nextInt(choices.length);
		choices[this.answer] = answer;
	}

	public boolean addChoice(String choice) {
		int idx = -1;
		for (int i=0; i<choices.length; ++i) {
			if (choices[i] == null) {
				if (idx < 0)
					idx = i;
			} else if (choices[i].equals(choice)){
				return false;
			}
		}
		if (idx >= 0)
			choices[idx] = choice;
		return true;
	}

	public void addNoneOfAboveChoice() {
		choices[choices.length-1] = "** NONE OF ABOVE. **";
		this.answer = choices.length-1;
	}

	public boolean checkAnswer(int answer) {
		if (answer < 0)
			answer = NONE_OF_ABOVE;
		return this.answer == answer;
	}
}
